package pers.cjr.demo.component.ws;

import pers.cjr.demo.component.ws.handler.DefaultHandler;
import pers.cjr.demo.component.ws.interceptor.DefaultInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

//@EnableWebSocket // [1]
//@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private DefaultHandler defaultSocketHandler;
    @Autowired
    private DefaultInterceptor defaultInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(defaultSocketHandler, "/websocket") // todo  [2]
                .addInterceptors(defaultInterceptor) // todo  [3]
                .setAllowedOrigins("*"); // 解决跨域问题 [4]
    }
}